package load;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.xml.datatype.Duration;

public class LoadTestThread implements Runnable {

	private long start;
	private int auctionsIntervalMilli, auctionDurationSec, updateIntervalMilli,
			bidsIntervalMilli;
	private boolean isRunning = true;
	private String host;
	private int tcpPort;
	private int clientId;

	private PrintWriter out;
	private BufferedReader in;

	public synchronized PrintWriter getOut() {
		return out;
	}

	public synchronized BufferedReader getIn() {
		return in;
	}

	public boolean isRunning() {
		return isRunning;
	}

	public void setRunning(boolean isRunning) {
		this.isRunning = isRunning;
	}

	public LoadTestThread(int id, String host, int tcpPort, long start,
			int auctionsPerMin, int auctionDuration, int updateIntervalSec,
			int bidsPerMin) {
		this.clientId = id;
		this.host = host;
		this.tcpPort = tcpPort;
		this.start = start;
		this.auctionsIntervalMilli = 60000 / auctionsPerMin;
		this.auctionDurationSec = auctionDuration;
		this.updateIntervalMilli = updateIntervalSec * 1000;
		this.bidsIntervalMilli = 60000 / bidsPerMin;
	}

	@Override
	public void run() {

		try {
			Socket serverSocket = new Socket(host, tcpPort);
			out = new PrintWriter(serverSocket.getOutputStream(),
					true);
			in = new BufferedReader(new InputStreamReader(
					serverSocket.getInputStream()));
			
			out.println("!login testClient" + clientId + " 123");
			in.readLine();

			Timer auctionTimer = new Timer();
			Timer updateTimer = new Timer();
			Timer bidTimer = new Timer();

			auctionTimer.schedule(new TimerTask() {

				@Override
				public void run() {
					createAuction();
				}

			}, 0, auctionsIntervalMilli);

			updateTimer.schedule(new TimerTask() {

				@Override
				public void run() {
					updateAuctions();
				}

			}, 0, updateIntervalMilli);

			bidTimer.schedule(new TimerTask() {

				@Override
				public void run() {
					placeBid();
				}

			}, 0, bidsIntervalMilli);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private synchronized void createAuction() {
		out.println("!create " + auctionDurationSec + " test");
		try {
			in.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private synchronized void updateAuctions() {
		out.println("!list");
		try {
			in.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private synchronized void placeBid() {
		
		try {
			out.println("!list");
			String auctions = in.readLine();
			if (auctions.contains("|")) {
				String[] split = auctions.split("\\|");
				int i =	(new Random()).nextInt(split.length);
				int id = Integer.parseInt(split[i].substring(0, split[i].indexOf('.')));
				long now = Calendar.getInstance().getTimeInMillis();
				
				out.println("!bid " + id + " " + (now - start));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
